import _mergeJSXProps7 from "@vue/babel-helper-vue-jsx-merge-props";
import _mergeJSXProps6 from "@vue/babel-helper-vue-jsx-merge-props";
import _mergeJSXProps5 from "@vue/babel-helper-vue-jsx-merge-props";
import _mergeJSXProps4 from "@vue/babel-helper-vue-jsx-merge-props";
import _mergeJSXProps3 from "@vue/babel-helper-vue-jsx-merge-props";
import _mergeJSXProps2 from "@vue/babel-helper-vue-jsx-merge-props";
import _mergeJSXProps from "@vue/babel-helper-vue-jsx-merge-props";

/* eslint-disable indent */
import { getSlot } from './props.util';
export default {
  name: 'HrUpload',
  props: {
    originFileList: {
      type: Array,
      default: () => []
    },
    size: {
      type: String,
      default: 'small'
    },
    disabled: {
      type: Boolean,
      default: false
    },
    readonly: {
      type: Boolean,
      default: false
    },
    immediate: {
      type: Boolean,
      default: false
    },
    limit: {
      type: [Number, String],
      default: undefined
    },
    user: {
      type: String,
      default: undefined
    },
    ignoreUser: {
      type: Boolean,
      default: false
    },
    fullInfo: {
      type: Boolean,
      default: true
    },
    fileList: {
      type: Array,
      default: () => []
    }
  },

  data() {
    return {};
  },

  model: {
    prop: "fileList",
    event: "change"
  },
  computed: {
    visibleFileList() {
      return [...this.originFileList.map(d => ({ ...d,
        isOrigin: true
      })), ...this.fileList];
    }

  },
  methods: {
    removeFile(file) {
      if (file.isOrigin) {
        this.$emit('on-remove', file);
        return;
      }

      const index = [].findIndex.call(this.fileList, d => d === file);
      const list = [...this.fileList];
      list.splice(index, 1);
      this.$emit('change', list);
    },

    clearFiles() {
      // this.$refs.upload && this.$refs.upload.clearFiles()
      this.$emit('change', []);
    }

  },

  render(h) {
    const tipSlot = getSlot(this, 'tip'); // const triggerSlot = getSlot(this, 'trigger')

    const uploadProps = {
      props: {
        accept: '.doc,.docx,.wps,.xls,.xlsx,.ppt,.pptx,.png,.jpg,.jpeg,.pdf',
        ...this.$attrs,
        fileList: this.fileList,
        onChange: (file, fileList) => {
          if (this.immediate) {
            this.$emit('change', [file]);
          } else {
            const start = Math.max(this.visibleFileList.length - (this.limit || 0), 0);
            this.$emit('change', this.limit ? fileList.slice(start, start + this.limit) : fileList);
          }
        }
      },
      scopedSlots: {
        tip: function (props) {
          return tipSlot;
        }
      },
      ref: 'upload'
    };
    return h("div", [this.disabled ? [] : h("el-upload", _mergeJSXProps([{
      "class": "hr-upload",
      "attrs": {
        "action": "/",
        "auto-upload": false,
        "show-file-list": false
      }
    }, uploadProps]), [h("el-button", {
      "slot": "trigger",
      "attrs": {
        "plain": true,
        "round": true,
        "size": "mini",
        "icon": "el-icon-plus"
      }
    }, ["\u9009\u62E9\u6587\u4EF6"])]), h("el-table", {
      "class": "w-100",
      "attrs": {
        "size": "mini",
        "data": this.visibleFileList,
        "empty-text": this.readonly ? '无数据' : '暂无数据'
      }
    }, [h("el-table-column", _mergeJSXProps3([{
      "attrs": {
        "label": "文件名"
      }
    }, {
      scopedSlots: {
        default: ({
          row
        }) => {
          const props = {
            class: 'text-reset'
          };

          if (row.isOrigin) {
            props.class = 'text-primary';
            props.on = {
              click: () => {
                this.$emit('link', row);
              }
            };
          }

          const ELE = [h("a", _mergeJSXProps2([{
            "attrs": {
              "role": "button"
            }
          }, props]), [row.attachmentName || row.name])]; // if (!row.isOrigin) {
          //   ELE.push(<span class="text-muted ml-2">待上传</span>)
          // }

          return ELE;
        }
      }
    }])), h("el-table-column", _mergeJSXProps4([{
      "attrs": {
        "label": "文件大小"
      }
    }, {
      scopedSlots: {
        default: ({
          row
        }) => {
          return h("span", [((row.attachmentSize || row.size) / 1024 || 0).toFixed(2) + 'KB']);
        }
      }
    }])), this.fullInfo ? [h("el-table-column", _mergeJSXProps5([{
      "attrs": {
        "label": "上传人"
      }
    }, {
      scopedSlots: {
        default: ({
          row
        }) => {
          return h("span", [row.creatorName || '-']);
        }
      }
    }])), h("el-table-column", _mergeJSXProps6([{
      "attrs": {
        "label": "时间"
      }
    }, {
      scopedSlots: {
        default: ({
          row
        }) => {
          return h("span", [row.createTime || '-']);
        }
      }
    }]))] : [], this.readonly ? [] : h("el-table-column", _mergeJSXProps7([{
      "attrs": {
        "label": "操作",
        "align": "center",
        "width": "180"
      }
    }, {
      scopedSlots: {
        default: ({
          row
        }) => {
          return this.ignoreUser || row.creatorId === this.user || !row.isOrigin ? h("el-link", {
            "attrs": {
              "type": "text"
            },
            "on": { ...{
                click: () => {
                  this.removeFile(row);
                }
              }
            }
          }, [h("i", {
            "class": "el-icon-delete"
          })]) : [];
        }
      }
    }]))])]);
  }

};